#include "door_operations.h"
#include "ui_door_operations.h"
#include "main.h"

/************************************
    设备类型      machine_type_id
   "方炉双门自动"     1
   "方炉单门自动"     2
   "方炉双门手动"     3
   "方炉单门手动"     4
   "双门圆炉"        5
   "单门圆炉"        6
************************************/
#define   START_TIMER \
    if( timer->isActive())   timer->stop();\
  if(machine_type_id==circular_double_door)   timer->start(10000);\
  else if(machine_type_id<3)  timer->start(30000);\
  else  timer->start(1000);

door_operations::door_operations(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::door_operations)
{

    ui->setupUi(this);
    timer=new QTimer();
   QObject:: connect( timer, SIGNAL(timeout()), SLOT(timeDone()) );
   timer->setSingleShot(true);
   timeDone();
   timer2=new QTimer();
   QObject:: connect( timer2, SIGNAL(timeout()), SLOT(timeDone2()) );
   timer2->start(1000);
  // hideunusedbutton();
   door_actions=0;
   setWindowFlags(Qt::FramelessWindowHint);
   ui->pressure_display->setpressure_display();
   door=new door_control;
   first=1;//跳过开机首次硬件加电后电平跳变导致的开关门信号
}

door_operations::~door_operations()
{
    delete ui;
}

void door_operations::enter()
{
   if(whole_machine_parameter.machine_type_id<circular_single_door)
   {
     hideunusedbutton();
     if(sterilizing &&  sterilization_protype!=MANUAL_OPERATION)
        {WARN->show_text("正在灭菌不能开门！");}
     else exec();
  }
}

 void door_operations::unlock_back_door(bool lock)
 {
    door->repair_mode=lock;

 }

void door_operations::timeDone()
{
    switch(door_actions)
    {
    case openning_front_door:
        if(front_door_opened_in_place)
       {
            ui->open_frontdoor_btn->setEnabled(false);
        }
        else
        {
            ui->open_frontdoor_btn->setEnabled(true);
            ui->open_frontdoor_btn->SET_COLOR_GREEN
         }
        break;
    case closing_front_door:
        if(front_door_closed_in_place)
       {
         if(front_door_Sealing_strip_pressure_in_place)
         {
          ui->close_frontdoor_btn->setEnabled(false);
          }
        }
        else
        {
         ui->close_frontdoor_btn->setEnabled(true);
         ui->close_frontdoor_btn->SET_COLOR_GREEN
         }
        break;
    case closing_back_door:
        if(back_door_closed_in_place)
       {
            if(back_door_Sealing_strip_pressure_in_place)
            {
             ui->close_backdoor_btn->setEnabled(false);
             }
        }
        else
        {
            ui->close_backdoor_btn->setEnabled(true);
            ui->close_backdoor_btn->SET_COLOR_GREEN
         }
        break;
     case openning_back_door:
        if(back_door_opened_in_place)
       {
         ui->open_backdoor_btn->setEnabled(false);
         ui->open_backdoor_btn->SET_COLOR_GRAY
        }
        else
        {
            ui->open_backdoor_btn->setEnabled(true);
            ui->open_backdoor_btn->SET_COLOR_GREEN
         }
        break;

     default://halting_door
        ui->halt_door_btn->SET_COLOR_GREEN break;
    }

      door_actions=0;
}

void door_operations::timeDone2()
{

//    if(PSU>-3 && PSU<3 && machine_type_id != circular_single_door)
//    {exhaust_valve_OFF; air_supply_valve_OFF;}

    if(front_door_closed_in_place)
   {
//     if(machine_type_id !=circular_double_door)
//     { ui->close_frontdoor_btn->setEnabled(false); }
     ui->frontdoor_lineEdit->setText(trUtf8("前门关闭"));
     ui->frontdoor_lineEdit ->setPalette(redtextpal);
     if(front_door_Sealing_strip_pressure_in_place)
     {
         ui->frontdoor_lineEdit->setText(trUtf8("前门已锁"));
         ui->frontdoor_lineEdit ->setPalette(bluetextpal);
         ui->close_frontdoor_btn->setEnabled(false);
         ui->open_frontdoor_btn->SET_COLOR_GREEN
     }
     else{
           ui->close_frontdoor_btn->setEnabled(true);
           ui->close_frontdoor_btn->SET_COLOR_GREEN
        }
    }
    else
    {
      ui->frontdoor_lineEdit->setText(trUtf8("前门未关"));
      ui->frontdoor_lineEdit ->setPalette(blacktextpal);
      ui->close_frontdoor_btn->setEnabled(true);
     // ui->close_frontdoor_btn->SET_COLOR_GREEN
      if(front_door_opened_in_place)
     {
       ui->open_frontdoor_btn->setEnabled(false);
       ui->open_frontdoor_btn->SET_COLOR_GRAY
      }
      else
      {
        ui->open_frontdoor_btn->setEnabled(true);
        ui->open_frontdoor_btn->SET_COLOR_GREEN
       }
     }

    if(back_door_closed_in_place)
   {
//        if(machine_type_id !=circular_double_door)
//        { ui->close_backdoor_btn->setEnabled(false); }
     ui->backdoor_lineEdit->setText(trUtf8("后门关闭"));
     ui->backdoor_lineEdit ->setPalette(redtextpal);
     if(back_door_Sealing_strip_pressure_in_place)
     {
         ui->backdoor_lineEdit->setText(trUtf8("后门已锁"));
         ui->backdoor_lineEdit ->setPalette(bluetextpal);
         ui->close_backdoor_btn->setEnabled(false);
         ui->open_backdoor_btn->SET_COLOR_GREEN
      }
     else{
          ui->close_backdoor_btn->setEnabled(true);
          ui->close_backdoor_btn->SET_COLOR_GREEN
         }
     }
    else
    {
         ui->backdoor_lineEdit->setText(trUtf8("后门未关"));
         ui->backdoor_lineEdit ->setPalette(blacktextpal);
         ui->close_backdoor_btn->setEnabled(true);
      if(back_door_opened_in_place)
     {
       ui->open_backdoor_btn->setEnabled(false);
       ui->open_backdoor_btn->SET_COLOR_GRAY
      }
      else
      {
        ui->open_backdoor_btn->setEnabled(true);
        ui->open_backdoor_btn->SET_COLOR_GREEN
       }
    }

    if(exhaust_valve_isopen)
   {
   ui->exhaust_btn->setText(trUtf8("正在排气"));
   ui->exhaust_btn->SET_COLOR_GREEN
    }
    else
    {
        ui->exhaust_btn->setText(trUtf8("排气"));
        ui->exhaust_btn->SET_COLOR_GRAY
    }

    if(air_supply_valve_isopen)
   {
   ui->air_back_btn->setText(trUtf8("正在回气"));
   ui->air_back_btn->SET_COLOR_GREEN
    }
    else
    {
        ui->air_back_btn->setText(trUtf8("回气"));
        ui->air_back_btn->SET_COLOR_GRAY
    }



    if(!sterilizing && door->rise_edge_true(overpressure_protection_switch_isreached)||
            door->down_edge_true(!overpressure_protection_switch_isreached) )
    {
        if(first)  first=0;
        else
       {
        if(overpressure_protection_switch_isreached) on_open_backdoor_btn_clicked();
        else on_close_backdoor_btn_clicked();
        }

    }
    if(workDone && !front_door_Sealing_strip_pressure_in_place ) workDone=0;
    //灭菌完成后开了前门就不能再开后门，防止细菌进入无菌室
}
void door_operations::on_close_frontdoor_btn_clicked()
{
    BUTTON_SOUND
    ui->close_frontdoor_btn->SET_COLOR_RED
    if(door_actions==closing_back_door) ui->close_backdoor_btn->SET_COLOR_GREEN

    door_actions=closing_front_door;
    if(whole_machine_parameter.machine_type_id==circular_double_door )
    {
    if(front_door_closed_in_place)
     {
     door->cmd_close_front_door();
    // msleep(500);
     }
    else{
        WARN->show_text("前门未关，不能锁定");
         }
    }
    else {
           door->cmd_close_front_door();
          // msleep(500);
          }
}


void door_operations::on_close_backdoor_btn_clicked()
{
     BUTTON_SOUND;
     ui->close_backdoor_btn->SET_COLOR_RED
     if(door_actions==closing_front_door) ui->close_frontdoor_btn->SET_COLOR_GREEN

     door_actions=closing_back_door;
     if(whole_machine_parameter.machine_type_id==circular_double_door )
     {
     if(back_door_closed_in_place)
     {
         door->cmd_close_back_door();
     }
     else{
             WARN->show_text("后门未关，不能锁定！");
          }
     }
     else {
              door->cmd_close_back_door();
            }
}


void door_operations::on_open_frontdoor_btn_clicked()
{
    BUTTON_SOUND;


        if(door->repair_mode || back_door_closed_in_place ||
                whole_machine_parameter.machine_type_id%2==0 )
        {
            if(PSU< MAX_OPEN_DOOR_PRESSURE && PSU > MIN_OPEN_DOOR_PRESSURE)
            {
                door->cmd_open_front_door();
                ui->open_frontdoor_btn->SET_COLOR_RED
                door_actions=openning_front_door;
            }
            else balance_inner_chamber_pressure();

        }
        else
        {
            if(!back_door_closed_in_place)
            WARN->show_text("后门已开，不能同时开两扇门！");

        }


}

void door_operations::balance_inner_chamber_pressure()
{
    if(PSU <= MIN_OPEN_DOOR_PRESSURE )
    {
        if( whole_machine_parameter.bottom_exhaust_machine) WARN->show_text("内有负压，请打开手动排气阀回气，回气后关闭！");
        else {air_supply_valve_ON  WARN->show_text("内有负压，正在回气！");}
    }
    else if(PSU >= MAX_OPEN_DOOR_PRESSURE ) {exhaust_valve_ON WARN->show_text("内有正压，正在排气！");}

}

void door_operations::on_open_backdoor_btn_clicked()
{
     BUTTON_SOUND;

    if( door->repair_mode ||(workDone && front_door_closed_in_place)) //灭菌完成才能开后门
   {
     if(PSU< MAX_OPEN_DOOR_PRESSURE && PSU > MIN_OPEN_DOOR_PRESSURE)
    {
    door->cmd_open_back_door();
    ui->open_backdoor_btn->SET_COLOR_RED
    door_actions=openning_back_door;
     }
     else balance_inner_chamber_pressure();

    }
    else
    {
          if(workDone)
          {
         if(!front_door_closed_in_place)
         {
             WARN->show_text("前门已开，不能同时开两扇门！");
             //workDone=0;
         }
          }
          else
          {
              WARN->show_text("灭菌未完成，不能开后门！");
          }
     }

}

void door_operations::on_halt_door_btn_clicked()
{
    BUTTON_SOUND;
    door_actions=halting_door;
    timer->stop();
    timeDone();
    timer->start(500);
    door->cmd_halt_door();  
    ui->close_frontdoor_btn->SET_COLOR_GREEN
    ui->open_frontdoor_btn->SET_COLOR_GREEN
    ui->close_backdoor_btn->SET_COLOR_GREEN
    ui->open_backdoor_btn->SET_COLOR_GREEN
    ui->halt_door_btn->SET_COLOR_RED
}


void door_operations::on_return_btn_clicked()
{
    BUTTON_SOUND;
    if(exhaust_valve_isopen)   exhaust_valve_OFF
    if(air_supply_valve_isopen) air_supply_valve_OFF
    this->close();
}



void door_operations::hideunusedbutton()
{
    if(whole_machine_parameter.machine_type_id%2 )//单门
    {
        ui->backdoor_lineEdit->show();

        if(door->repair_mode || workDone)
        {
           // ui->close_backdoor_btn->show();
            ui->open_backdoor_btn->show();
        }
        else
        {
           // ui->close_backdoor_btn->hide();
            ui->open_backdoor_btn->hide();
        }
    }
    else
    {

      ui->backdoor_lineEdit->hide();
      ui->close_backdoor_btn->hide();
      ui->open_backdoor_btn->hide();
    }

    switch(whole_machine_parameter.machine_type_id)
    {
    case square_auto_double_door:
    case square_auto_single_door://("方炉单门自动");
        ui->halt_door_btn->show();
        break;
    default://方炉双门手动、圆炉双门
            ui->halt_door_btn->hide();
        break;

    }

    if( door->repair_mode && whole_machine_parameter.machine_type_id< 3 ) ui->wear_btn->show();
    else ui->wear_btn->hide();
}

void door_operations::on_air_back_btn_clicked()
{
      BUTTON_SOUND;
       if(ui->air_back_btn->isChecked())
      {
      ui->air_back_btn->SET_COLOR_GREEN
      ui->air_back_btn->setText(trUtf8("正在回气"));
      air_supply_valve_ON;
       }
       else
       {
       air_supply_valve_OFF;
       ui->air_back_btn->setText(trUtf8("回气"));
       ui->air_back_btn->SET_COLOR_GRAY
       }
}

void door_operations::on_exhaust_btn_clicked()
{
     BUTTON_SOUND;
      if(ui->exhaust_btn->isChecked())
     {
         ui->exhaust_btn->SET_COLOR_GREEN
         exhaust_valve_ON;
         ui->exhaust_btn->setText(trUtf8("正在排气"));
      }
      else
      {
          exhaust_valve_OFF;
          ui->exhaust_btn->SET_COLOR_GRAY
          ui->exhaust_btn->setText(trUtf8("排气"));
      }
}


void door_operations::on_wear_btn_clicked()
{
    static bool b;
    if(b) {door->testtimer->start(30000); b=false;ui->wear_btn->SET_COLOR_RED}
    else {door->testtimer->stop(); b=true;ui->wear_btn->SET_COLOR_GRAY}

}
